//
//  ViewController.m
//  数据存储
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *ids;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *age;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path=NSHomeDirectory();
    NSLog(@"%@",path);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)save1:(id)sender
{
    NSString *ids=self.ids.text;
     NSString *name=self.name.text;
    NSString *age=self.age.text;
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    [user setObject:ids forKey:@"ids"];
    [user setObject:name forKey:@"name"];
    [user setObject:age forKey:@"age"];
    
    
    
    
}


- (IBAction)read1:(id)sender
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord1 = [ user objectForKey:@"ids"];
    self.ids.text=passWord1;
    NSString *passWord2 = [ user objectForKey:@"name"];
    self.name.text=passWord2;
    NSString *passWord3 = [ user objectForKey:@"age"];
    self.age.text=passWord3;
    
}

- (IBAction)save2:(id)sender
{
     NSString *path=NSHomeDirectory();
    NSString *dochpath=[path stringByAppendingPathComponent:@"Documents"];
    NSString *filepath=[dochpath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *student=@{@"ids":self.ids.text,
                            @"name":self.name.text,
                            @"age":self.age.text};
    [student writeToFile:filepath atomically:YES ];
    
}
- (IBAction)read2:(id)sender
{
    NSString *path=NSHomeDirectory();
    NSString *dochpath=[path stringByAppendingPathComponent:@"Documents"];
    NSString *filepath=[dochpath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *student=[NSDictionary dictionaryWithContentsOfFile:filepath];
    self.ids.text=student[@"ids"];
    self.name.text=student[@"name"];
    self.age.text=student[@"age"];
    
}
- (IBAction)save3:(id)sender
{
    // 存取位置
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    NSLog(@"存储位置:%@",filePath);
    
    // 1. 将信息存储到对象中
    StudentModel *m = [[StudentModel alloc] initWithId:self.ids.text
                                               andName:self.name.text
                                                andAge:self.age.text];
    // 2. 创建可变数据对象用于归档存入
    NSMutableData *mData = [NSMutableData data];
    // 3. 创建归档对象
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    // 4. 归档StuentModel对象
    [arch encodeObject:m forKey:@"Student"];
    // 5. 完成归档
    [arch finishEncoding];
    // 6. 数据存入文件
    [mData writeToFile:filePath atomically:YES];
    
    
}

- (IBAction)read3:(id)sender
{
    // 存取位置
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    NSLog(@"读取位置:%@",filePath);
    
    // 1. 读出数据(二进制数据)
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    // 2. 创建解档对象
    NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    // 3. 从解档对象中解档Student(和归档时使用相同的key)
    StudentModel *stu = [unArch decodeObjectForKey:@"Student"];
    // 4. 完成解档
    [unArch finishDecoding];
    // 5. 将信息显示到界面
    self.ids.text = stu.stuId;
    self.name.text = stu.stuName;
    self.age.text = stu.stuAge;
    
    
    
}








@end
